/**
 * 
 */
package xj.toolkit.fsm;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xj.toolkit.fsm.annotation.StateDescription;


/**
 * @author hp
 *
 */
public class FSMUtils {
	
    private static final Logger logger = 
    	LoggerFactory.getLogger(FSMUtils.class);
    
    public static String getStateNameOfInstance(State state) {
    	StateDescription desc = state.getClass().getAnnotation(StateDescription.class);
    	if ( null != desc ) {
        	return	desc.name();
    	}
    	if ( PropertyUtils.isReadable(state, "name") ) {
	    	try {
				return	BeanUtils.getSimpleProperty(state, "name");
			} catch (Exception e) {
				logger.error("getStateNameOfInstance:", e);
				return	null;
			}
		}
    	
    	return	null;
    }
    
    public static String getStateNameOfType(Class<? extends State> stateType) {
    	StateDescription desc = stateType.getAnnotation(StateDescription.class);
    	
    	return	desc.name();
    }
}
